#ifndef __FIND_NEI_
#define __FIND_NEI_
#include <map>
#include "common.h"
using namespace std;

class CFindNei
{
public:
	CFindNei() = default;
	~CFindNei();
	bool Init(const float cellSize);
	void Clear();
	void AddItem(const unsigned short id,
		const float minx, const float miny,
		const float maxx, const float maxy);

	int QueryItems(const float minx, const float miny,
		const float maxx, const float maxy,
		unsigned short* ids, const int maxIds);

	int GetItemCountAt(const int x, const int y);

private:
	struct Item
	{
		unsigned short id;
		short x, y;
	};
	typedef multimap<int, Item> MapKey;
	MapKey m_hashMap;

	float m_cellSize;
	float m_invCellSize;
	int m_bucketsSize;
	const int hashSeed = 128;
	int m_bounds[4];
private:
	inline int hashPos2(int x, int y, int n);
	unsigned int dtNextPow2(unsigned int v);
private:
	int operator=(CFindNei const&);
};
#endif 

